package com.tencent.sr.iris.basic.saas.common.utils;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class CommonUtils {

    /**
     * 手机号脱敏
     *
     * @param phone
     * @return
     */
    public static String phoneUnSensitive(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return "";
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    public static Double longToDouble(Long num) {
        if (Objects.nonNull(num)) {
            return (double) num / 100;
        } else {
            return 0.0;
        }
    }

    public static Double integerToDouble(Integer num) {
        if (Objects.nonNull(num)) {
            return (double) num / 100;
        } else {
            return 0.0;
        }
    }

    /**
     * 校验手机号是否为正常国内手机号
     */
    public static boolean checkNormalDomesticPhone(String phone) {
        if (!StringUtils.isNumeric(phone)) {
            return false;
        }
        String regExp = "1[3|4|5|6|7|8|9][0-9]\\d{8}";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);
        return m.matches();
    }
}

